/**
 * 
 */
package com.cea.papyrus.diagram.component.figure;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Defines the view of an deployment specification.
 * 
 * @author al090761
 *
 */
public class BasicArtifactFigure extends NodeFigure {
	/**
	 * The Class ActorLabelFigure.
	 * Holds the names and stereotypes.
	 */
	protected class BasicArtifactLabelFigure extends NodeFigure {
		
		/**
		 * The Constructor.
		 */
		public BasicArtifactLabelFigure() {
			super();
			ToolbarLayout layout = new ToolbarLayout(ToolbarLayout.VERTICAL); // vertically
			layout.setStretchMinorAxis(true);
			layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
			layout.setSpacing(2);
			setLayoutManager(layout);
		}
	
		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics){
			if (isNameUnderlined) {
				graphics.setForegroundColor(labelFigure.getFontColor());
				Rectangle rectLabel = labelFigure.getBounds();
				Dimension labelSize=FigureUtilities.getTextExtents(labelFigure.getNameLabel().getText(), labelFigure.getNameLabel().getFont());
				graphics.drawLine(rectLabel.x+rectLabel.width/2-labelSize.width/2,
						rectLabel.y+rectLabel.height-2,
						rectLabel.x+rectLabel.width/2+labelSize.width/2,
						rectLabel.y+rectLabel.height-2);
			}
		}
	}
	
	/**
	 * The Class ActorLabelFigure.
	 * Holds the names and stereotypes.
	 */
	protected class BasicArtifactBorderFigure extends Figure {

		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics){
			if (isBorderRequired){
				setForegroundColor(getBorderColor());
				Point topLeft = getBounds().getTopLeft();
				Point topRight = getBounds().getTopRight();
				graphics.drawLine(topLeft.x,topLeft.y + BORDER_MARGIN,topRight.x,topRight.y + BORDER_MARGIN);
			}	
		}

		/**
		 * The Constructor.
		 */
		public BasicArtifactBorderFigure() {
			super();
			setSize(new Dimension(10,2*BORDER_MARGIN));
		}
	}

	/**
	 * 
	 */
	protected static final int BORDER_MARGIN = 5;

	/**
	 * The label figure.
	 */
	protected BasicArtifactLabelFigure labelFigure;
	
	/**
	 * The is name underlined.
	 */
	protected boolean isNameUnderlined = false;

	/**
	 * 
	 */
	protected BasicArtifactBorderFigure borderFigure;

	/**
	 * 
	 */
	protected boolean isBorderRequired = false;
	
	/**
	 * The Constructor.
	 * 
	 * @param keyword 
	 */
	public BasicArtifactFigure(String keyword){
		super();
		super.setName(PapyrusPlugin.ST_LEFT+keyword+PapyrusPlugin.ST_RIGHT);
		ToolbarLayout layout = new ToolbarLayout(ToolbarLayout.VERTICAL);
		layout.setStretchMinorAxis(true);
		layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
		setLayoutManager(layout);	
		labelFigure = new BasicArtifactLabelFigure();
		add(labelFigure);
		borderFigure = new BasicArtifactBorderFigure();
		add(borderFigure);
	}

	/**
	 * Gets the name label.
	 * 
	 * @return Returns the nameLabel.
	 */
	@Override
	public Label getNameLabel() {
		return labelFigure.getNameLabel();
	}

	/**
	 * Gets the qualified name label.
	 * 
	 * @return Returns the qualifiedNameLabel.
	 */
	@Override
	public Label getQualifiedNameLabel() {
		return labelFigure.getQualifiedNameLabel();
	}

	/**
	 * Gets the stereotypes label.
	 * 
	 * @return Returns the nameLabel.
	 */
	@Override
	public Label getStereotypesLabel() {
		return labelFigure.getStereotypesLabel();
	}

	/**
	 * Refresh colors.
	 * 
	 * @param graphElement the graph element
	 */
	@Override
	public void refreshColors(GraphElement graphElement) {
		super.refreshColors(graphElement);
		labelFigure.setFontColor(getFontColor());
		labelFigure.setBorderColor(getBorderColor());
		labelFigure.setForeGroundColor(getForeGroundColor());
		setFont(PapyrusPlugin.fontManager.get(new FontData[]{new FontData(graphElement.getFontFamily(),graphElement.getFontSize(),SWT.NORMAL)}));
		Font baseFont = super.getNameLabel().getFont();
		if (baseFont.getFontData() != null){
			FontData baseFontData = baseFont.getFontData()[0];
			FontData[] labelFontData={new FontData(baseFontData.getName(), baseFontData.getHeight(), SWT.BOLD)};
			Font labelFont = PapyrusPlugin.fontManager.get(labelFontData);			
			labelFigure.setFont(labelFont);
		}
	}

	/**
	 * Refresh presentation.
	 * 
	 * @param presentation the presentation
	 * @param selection the selection
	 */
	@Override
	public void refreshPresentation(String presentation, Element selection, GraphElement ge) {
		labelFigure.refreshPresentation(presentation, selection, ge);
	}

	/**
	 * Sets the name.
	 * 
	 * @param name the name
	 */
	@Override
	public void setName( String name )
	{
		labelFigure.setName(name);
	}

	/**
	 * Sets the qualified name.
	 * 
	 * @param packageName the package name
	 */
	@Override
	public void setQualifiedName( String packageName )
	{
		labelFigure.setQualifiedName(packageName);
	}

	/**
	 * Set the stereotype value.
	 * If param is null, remove the label.
	 * 
	 * @param stereotypes the stereotypes
	 */
	@Override
	public void setStereotypes( String stereotypes )
	{
		labelFigure.setStereotypes(stereotypes);
	}

	/**
	 * 
	 * 
	 * @param isNameUnderlined 
	 */
	public void setNameUnderlined(boolean isNameUnderlined) {
		this.isNameUnderlined = isNameUnderlined;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public BasicArtifactLabelFigure getLabelFigure() {
		return labelFigure;
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	public Label getKeywordFigure() {
		return super.getNameLabel();
	}

	/**
	 * 
	 * 
	 * @param isBorderRequired 
	 */
	public void setBorderRequired(boolean isBorderRequired) {
		this.isBorderRequired = isBorderRequired;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public BasicArtifactBorderFigure getBorderFigure() {
		return borderFigure;
	}
}
